package com.example.sale.Controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sale.Service.IProductService;
import com.example.sale.Service.imply.ProductService;
import com.example.sale.True.Product;
import com.example.sale.utils.UploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("product")
@Slf4j
public class ProductListController {
    @Autowired
    private IProductService iProductService;
    @Autowired
    private ProductService productService;
  @PostMapping("/upImg")
  public String upImg(MultipartFile file) throws IOException {
      return UploadUtil.uploadImage(file);
  }
  @PostMapping("/post")
  public Map<String,Object> postProduct(@RequestBody Product product){
      System.out.println(product);
      productService.clearCache();
      iProductService.save(product);
      Map<String,Object> result = new HashMap<>();
      result.put("state","success");
      return result;
  }
    @GetMapping("/search")
//    @Cacheable(value = "pageDataProduct", key = "#pageNO + ':' + #name + ':' + #address + ':' + #age+ ':' + #pageSize")
    @Cacheable(value = "pageDataProduct", key = "#pageNO + ':' + #name + ':' + #type + ':' + #price + ':' + #pageSize")
    public Map<String,Object>SearchData(
            @RequestParam(value ="name",required = false)String name,
            @RequestParam(value ="type",required = false)String type,
            @RequestParam(value = "price",required = false)String price,
            @RequestParam(value = "pageNo") Integer pageNO,
            @RequestParam(value = "pageSize") Integer pageSize)
    {
        System.out.println("name is"+name);
        System.out.println("type is"+type);
        System.out.println("price is"+price);
        System.out.println("pageNo is"+pageNO);
        System.out.println("pageSize is "+pageSize);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(name),Product::getName,name);
        wrapper.eq(StringUtils.hasLength(type),Product::getType,type);
        wrapper.eq(StringUtils.hasLength(price),Product::getPrice,price);
        Page<Product> page =new Page<>(pageNO,pageSize);
        Map<String,Object> result = new HashMap<>();
        iProductService.page(page,wrapper);
        result.put("status",page.getTotal());
        result.put("data",page.getRecords());
        return result;
    }
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteData(@PathVariable Integer id){
        Map<String,Object> result = new HashMap<>();
        productService.clearCache();
        result.put("id",iProductService.removeById(id));
        return result;
    }
    @PostMapping  ("DeleteByIds")
    public Map<String,Object>deleteByIds(@RequestBody List<Integer> Ids){
        System.out.println(Ids);
        productService.clearCache();
        iProductService.removeByIds(Ids);
        Map<String,Object> result = new HashMap<>();
        result.put("data","success");
        return result;
    }
    @PutMapping("/putData")
    public Map<String, Object> changeData(@RequestBody Product product){
        Map<String, Object> response = new HashMap<>();
        productService.clearCache();
        response.put("boolean", iProductService.updateById(product));
        response.put("stata", "success");
        return response;
    }
    @PostMapping("/CheckExistProduct")
    public Map<String,Object> postProduct(@RequestParam String name){
        System.out.println(name);
        System.out.println("检验商品是否存在");
        System.out.println(name);
        Map<String,Object> result = new HashMap<>();
        boolean productExists = iProductService.isProductExists(name);
        System.out.println(productExists);
        result.put("status",productExists);
        result.put("data",666);
        System.out.println(result);
        return result;
    }
    @GetMapping("/{id}")
    public Map<String, Object> getBYid(@PathVariable Integer id){
        Map<String,Object> result = new HashMap<>();
        result.put("data",iProductService.getById(id));
        return result;
    }
    @GetMapping("/product")
    public List<Map<String, Object>> getProductData() {
        List<Map<String, Object>> dataList = new ArrayList<>();
        String meat = "肉类";
        String seafood = "水产类";
        String fruit = "水果类";
        String vegetable = "蔬菜类";

        Map<String, Object> product1 = new HashMap<>();
        product1.put("product", "肉类");
        product1.put("quantity", iProductService.GetMeatNumber(meat));
        dataList.add(product1);

        Map<String, Object> product2 = new HashMap<>();
        product2.put("product", "水产类");
        product2.put("quantity", iProductService.GetSeafoodNumber(seafood));
        dataList.add(product2);

        Map<String, Object> product3 = new HashMap<>();
        product3.put("product", "水果类");
        product3.put("quantity", iProductService.GetFruitNumber(fruit));
        dataList.add(product3);

        Map<String, Object> product4 = new HashMap<>();
        product4.put("product", "蔬菜类");
        product4.put("quantity", iProductService.GetVegetablesNumber(vegetable));
        dataList.add(product4);

        int allProduct = iProductService.GetMeatNumber(meat) + iProductService.GetSeafoodNumber(seafood) + iProductService.GetFruitNumber(fruit) + iProductService.GetVegetablesNumber(vegetable);

        Map<String, Object> totalProduct = new HashMap<>();
        totalProduct.put("product", "商品总数");
        totalProduct.put("quantity", allProduct);
        dataList.add(totalProduct);

        return dataList;
    }
    @GetMapping("/productPrice")
    public List<Map<String, Object>> getProductPriceData() {
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> product1 = new HashMap<>();
        product1.put("type", "最低价");
        product1.put("price", iProductService.GetMinPrice());
        dataList.add(product1);

        Map<String, Object> product2 = new HashMap<>();
        product2.put("type", "最高价");
        product2.put("price", iProductService.GetMaxPrice());
        dataList.add(product2);

        Map<String, Object> product3 = new HashMap<>();
        product3.put("type", "平均价");
        double avgPrice = iProductService.GetAvgPrice();
        String formattedAvgPrice = String.format("%.2f", avgPrice).replaceAll("0*$", "").replaceAll("\\.$", "");
        product3.put("price", formattedAvgPrice);
        dataList.add(product3);

        Map<String, Object> product4 = new HashMap<>();
        product4.put("type", "水果类");
        double price4 = iProductService.GetTypePrice("水果类");
        String formattedPrice4 = String.format("%.2f", price4).replaceAll("0*$", "").replaceAll("\\.$", "");
        product4.put("price", formattedPrice4);
        dataList.add(product4);

        Map<String, Object> product5 = new HashMap<>();
        product5.put("type", "蔬菜类");
        double price5 = iProductService.GetTypePrice("蔬菜类");
        String formattedPrice5 = String.format("%.2f", price5).replaceAll("0*$", "").replaceAll("\\.$", "");
        product5.put("price", formattedPrice5);
        dataList.add(product5);

        Map<String, Object> product6 = new HashMap<>();
        product6.put("type", "水产类");
        double price6 = iProductService.GetTypePrice("水产类");
        String formattedPrice6 = String.format("%.2f", price6).replaceAll("0*$", "").replaceAll("\\.$", "");
        product6.put("price", formattedPrice6);
        dataList.add(product6);

        Map<String, Object> product7 = new HashMap<>();
        product7.put("type", "肉类");
        double price7 = iProductService.GetTypePrice("肉类");
        String formattedPrice7 = String.format("%.2f", price7).replaceAll("0*$", "").replaceAll("\\.$", "");
        product7.put("price", formattedPrice7);
        dataList.add(product7);
        return dataList;
    }
}
